package com.common.util.jar;

public class JarUtilTools extends AbstractJarUtil {

	private static Class clazz = JarUtilTools.class;

	/**
	 * 
	 * @param clazz
	 */
	private static void init(Class clazz) {
		String path = clazz.getProtectionDomain().getCodeSource().getLocation().getFile();
		try {
			path = java.net.URLDecoder.decode(path, "UTF-8");
		} catch (java.io.UnsupportedEncodingException ex) {
			debug(clazz, ex);
		}
		java.io.File jarFile = new java.io.File(path);
		jarName = jarFile.getName();
		java.io.File parent = jarFile.getParentFile();
		if (parent != null) {
			jarPath = parent.getAbsolutePath();
		}
	}

	/**
	 * 获取Class类所在Jar包的名称
	 * 
	 * @return Jar包名 (例如：C:\temp\demo.jar 则返回 demo.jar )
	 */
	public static String getJarName(Class clazz) {
		init(clazz);
		try {
			return java.net.URLDecoder.decode(jarName, "UTF-8");
		} catch (java.io.UnsupportedEncodingException ex) {
			debug(clazz, ex);
		}
		return null;
	}

	/**
	 * 取得Class类所在的Jar包路径
	 * 
	 * @return 返回一个路径 (例如：C:\temp\demo.jar 则返回 C:\temp )
	 */
	public static String getJarPath(Class clazz) {
		init(clazz);
		try {
			return java.net.URLDecoder.decode(jarPath, "UTF-8");
		} catch (java.io.UnsupportedEncodingException ex) {
			debug(clazz, ex);
		}
		return null;
	}
}
